package com.lsx143.realtime.util;


import com.lsx143.realtime.bean.OrderInfo;
import com.lsx143.realtime.common.Constants;
import org.apache.commons.beanutils.BeanUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * JDBC工具类
 */
public class JdbcUtil {
    /**
     * 获取指定驱动包的JDBC连接
     *
     * @param driver 驱动包的全类名
     * @param url    连接地址
     * @return jdbc连接器
     * @throws ClassNotFoundException 异常
     * @throws SQLException           异常
     */
    public static Connection getJdbcConnection(String driver, String url) throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        return DriverManager.getConnection(url);
    }

    /**
     * 获取Phoenix连接
     *
     * @return Phoenix的连接
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static Connection getPhoenixConnection() throws SQLException, ClassNotFoundException {
        return getJdbcConnection(Constants.PHOENIX_DRIVER, Constants.PHOENIX_URL);
    }
   /* public static void main(String[] args) throws SQLException, ClassNotFoundException {
        List<OrderInfo> orderInfos = queryList(getJdbcConnection(
                "com.mysql.cj.jdbc.Driver",
                "jdbc:mysql://hadoop162:3306/gmall2021?user=root&password=aaaaaa"),
                "select * from order_info where id=?",
                new Object[]{"27661"},
                OrderInfo.class);
        System.out.println(orderInfos.toString());
    }*/

    /**
     * 返回指定JDBC连接执行sql的返回结果
     *
     * @param con  指定的JDBC连接
     * @param sql  待执行的SQL
     * @param args 选择的列
     * @param cls  最终输出的类型的类
     * @param <T>  最后查询输出的泛型
     * @return 指定类型的集合
     */
    public static <T> List<T> queryList(Connection con,
                                        String sql,
                                        Object[] args,
                                        Class<T> cls) {
        List<T> result = new ArrayList<>();
        //1.执行指定sql
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            //2.指定查询的列
            for (int i = 0; i < args.length; i++) {
                //替换占位符
                ps.setObject(i + 1, args[i]);
            }
            //3.获取结果和元数据
            ResultSet resultSet = ps.executeQuery();
            ResultSetMetaData metaData = ps.getMetaData();
            //4.进行反射
            while (resultSet.next()) {
                T t = cls.newInstance();
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    String columnName = metaData.getColumnLabel(i);
                    Object value = resultSet.getObject(i);
                    BeanUtils.setProperty(t, columnName, value);
                }
                result.add(t);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}
